package com.smartcruise.main.mainFragment.tools;

import android.content.Intent;
import android.net.Uri;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.base.BaseTitleFragment;
import com.smartcruise.base.TitleBuilder;
import com.smartcruise.data.Contacts;
import com.smartcruise.util.customView.TimeCountView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FengChaoQun
 * on 2018/1/21 0021
 * 救助界面
 */

public class SosFragment extends BaseTitleFragment {
    @BindView(R.id.timeCountView)
    TimeCountView timeCountView;
    @BindView(R.id.contactsInfo)
    TextView contactsInfo;

    private String phone;

    @Override
    protected void init() {
        setCloseActivity();
        Contacts emergency = realm.where(Contacts.class)
                .equalTo("isEmergency", true)
                .findFirst();
        if (emergency == null) {
            showToast("没有紧急联系人");
            getActivity().finish();
            return;
        }
        phone = emergency.getPhoneNumber();
        contactsInfo.setText(emergency.getUserName() + "  " + phone);
        timeCountView.setTimeCountCallBack(new TimeCountView.TimeCountCallBack() {
            @Override
            public void onTimeEnd() {
                call();
            }
        });
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_container_sos;
    }

    @Override
    protected TitleBuilder getTitleBuilder() {
        return TitleBuilder.create(true, getString(R.string.sosByOneButton));
    }

    @OnClick(R.id.cancel)
    public void onCancelClicked() {
        getActivity().finish();
    }

    @OnClick(R.id.call)
    public void onCallClicked() {
        call();
    }

    private void call() {
        //直接拨打电话
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
        startActivity(intent);
        getActivity().finish();
    }

}
